# encoding=utf-8
from playwright.sync_api import sync_playwright
import time
import redis
import pymysql
import json
import redis
from connect_db import ConnectMysqldb

redis_con = redis.Redis(host='127.0.0.1',
                        port=6379, db=2, decode_responses=True)

conn = pymysql.connect(host="localhost",
                       user='root',
                       port=3306,
                       password='123456',
                       database='big_data')
cur = conn.cursor()
config = {'mysql_host': "localhost",
          'mysql_user': 'root',
          'mysql_port': 3306,
          'mysql_passwd': '123456',
          'mysql_db': 'big_data'
          }


def select_gonkaihao():
    sql = r'''SELECT distinct gonkaihao FROM `agent_gonkaihao` WHERE
gonkaihao not in (SELECT distinct gonkaihao FROM pantent_status_gkh )'''
    cur.execute(sql)
    data = cur.fetchall()
    gkaihao_list = [sqh[0] for sqh in data]
    return gkaihao_list


def run(inst):
    with sync_playwright() as p:
        # browser = p.firefox.launch(headless=False)
        import json
        storage_state_path = f'./auth/{inst}_state.json'
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'ACCESS_TOKEN':
                        authorization = item.get('value')
        print(f'成功读取{inst}登录信息:{authorization}')
        browser = p.firefox.launch()
        context = browser.new_context(
            base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path)
        context.set_default_timeout(600000)
        context.set_default_navigation_timeout(600000)
        page = context.new_page()
        page.set_default_timeout(300000)
        page.set_default_navigation_timeout(300000)
        page.goto(
            'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
        page.wait_for_load_state('networkidle')
        max_page = 2
        for page_num in range(1, max_page + 1):
            print(f'@@开始抓取第{page_num}页@@')
            api_request_context = context.request
            headers = {
                "Accept": "application/json, text/plain, */*",
                "Content-Type": "application/json;charset=utf-8",
                "Authorization": 'Bearer ' + authorization,
                "userType": "USER_RZ_DAILIJIG"
            }
            data = {
                "zhuanlilx": "",
                "page": page_num,
                "size": 1000,
                "zhuanlisqh": "",
                "sortDataName": "",
                "sortType": "",
                "shenqingrxm": "华中科技大学同济医学院附属协和医院",
                'searchType': "dljg"

            }
            # sqxx,fyxx
            response = api_request_context.post(
                '/api/search/undomestic/publicSearch', data=data, headers=headers, timeout=0)
            res_data = response.json()
            data_list = res_data.get('data').get('records')
            try:
                sqh_list = [item.get('zhuanlisqh') for item in data_list]
                gongkaiggr_list = [item.get('gongkaiggr')
                                   for item in data_list]
            except:
                sqh_list = []
                gongkaiggr_list = []
            page.reload()
            page.wait_for_load_state('networkidle')
            cur.execute('select distinct sqh from pantent_list_0104')
            data = cur.fetchall()
            have_already_done = set([sqh[0].replace("ZL", '') for sqh in data])
            for sqh, gongkaiggr in zip(sqh_list, gongkaiggr_list):
                if sqh not in have_already_done:
                    sqxx_response = api_request_context.post(
                        '/api/view/gn/sqxx', data={'zhuanlisqh': sqh}, headers=headers, timeout=0)
                    patent_status_res_json = sqxx_response.json()
                    patent_status_data = patent_status_res_json.get('data')
                    item = dict()
                    item["sqh"] = sqh
                    try:
                        item["state"] = patent_status_data.get(
                            'zhuluxmxx').get('zhuluxmxx').get('anjianywzt')
                    except:
                        item["state"] = ''
                    try:
                        sq_date = patent_status_data.get(
                            'zhuluxmxx').get('zhuluxmxx').get('zhuanlimc')
                    except:
                        sq_date = ''
                    try:
                        item["sq_date"] = patent_status_data.get(
                            'zhuluxmxx').get('zhuluxmxx').get('shenqingr')
                    except:
                        item["sq_date"] = ''
                    try:
                        dljg_info = patent_status_data.get(
                            'dailijg').get('dailijgList')[-1]
                        item["dljg"] = dljg_info.get('dailijgdm')
                        item["dlr"] = dljg_info.get('diyidlrxm')
                    except:
                        item["dljg"] = ''
                        item["dlr"] = ''
                    famingrenList = patent_status_data.get(
                        'famingren').get('famingrenList')
                    try:
                        sqr_list = [line.get('famingrxm')
                                    for line in famingrenList]
                    except:
                        sqr_list = []
                    try:
                        shenqingrenList = patent_status_data.get(
                            'shenqingren').get('shenqingrenList')
                        item["sqr"] = ','.join([line.get('shenqingrxm')
                                                for line in shenqingrenList])
                    except:
                        item["sqr"] = ''
                    item["fmr_name"] = ','.join(sqr_list)
                    try:
                        item['fm_class'] = patent_status_data.get(
                            'zhuluxmxx').get('zhuluxmxx').get('zhufenlh')
                    except:
                        item['fm_class'] = ''
                    # print(item)
                    # print('====' * 10)
                    sql = f"""INSERT INTO pantent_list_0104 (gongkaiggr,sqh, case_status, fm_name,fm_class, sq_date,  agency, patent_user, first_agent, fmr,create_time) VALUES ('{gongkaiggr}','{item["sqh"]}', '{item["state"]}', '{sq_date}','{item["fm_class"]}',  '{item["sq_date"]}', '{item["dljg"]}', '{item["sqr"]}', '{item["dlr"]}', '{item["fmr_name"]}',NOW())"""
                    print(sql)
                    cur.execute(sql)
                    conn.commit()
                    page.reload()


def request_page_list(context, page_num, authorization, shenqingrxm, search_type):
    # 发送请求获取每页专利明细
    api_request_context = context.request
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json;charset=utf-8",
        "Authorization": 'Bearer ' + authorization,
        "userType": "USER_RZ_DAILIJIG"
    }
    if search_type == 'public':
        data = {
            "zhuanlilx": "",
            "page": page_num,
            "size": 1000,
            "zhuanlisqh": "",
            "sortDataName": "",
            "sortType": "",
            "shenqingrxm": shenqingrxm
        }
        # 公众查询
        page_response = api_request_context.post(
            'https://cpquery.cponline.cnipa.gov.cn/api/search/undomestic/publicSearch', data=data, headers=headers, timeout=0)
    elif search_type == 'dljg':
        data = {
            "zhuanlilx": "",
            "page": page_num,
            "size": 1000,
            "zhuanlisqh": "",
            "sortDataName": "",
            "sortType": "",
            "shenqingrxm": shenqingrxm,
            'searchType': "dljg"
        }
        # 代理机构查询
        page_response = api_request_context.post(
            'https://cpquery.cponline.cnipa.gov.cn/api/search/domestic/certifiedSearch', data=data, headers=headers, timeout=0)
    elif search_type == 'sqr':
        data = {
            "zhuanlilx": "",
            "page": page_num,
            "size": 1000,
            "zhuanlisqh": "",
            "sortDataName": "",
            "sortType": "",
            "shenqingrxm": shenqingrxm,
            'searchType': 'sqr'
        }
        # 申请人查询
        page_response = api_request_context.post(
            '/api/search/domestic/certifiedSearch', data=data, headers=headers, timeout=0)
    page_json = page_response.json()
    status = page_json.get('status')
    print(f'---{shenqingrxm}_pagenum:{page_num}_status:{status}---')
    return(page_json)


def parse_page(page_json, shenqingrxm):
    # 解析每页专利详细信息
    patent_info_list = page_json.get('data').get('records')
    import pandas as pd
    df = pd.DataFrame(patent_info_list)
    df['yiyuan'] = shenqingrxm
    with ConnectMysqldb(config) as conn:
        df.to_sql('yiyuan_pantent_list_0104', conn,
                  if_exists='append', index=False)


def run_gonkai(inst, port):
    with sync_playwright() as p:
        # browser = p.firefox.launch(headless=False)
        import json
        storage_state_path = f'./auth/{inst}_state.json'
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'ACCESS_TOKEN':
                        authorization = item.get('value')
        print(f'成功读取{inst}登录信息:{authorization}')
        browser = p.firefox.launch()
        context = browser.new_context(
            base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path)
        context.set_default_timeout(300000)
        context.set_default_navigation_timeout(300000)
        page = context.new_page()
        page.set_default_timeout(300000)
        page.set_default_navigation_timeout(300000)
        page.goto(
            'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
        page.wait_for_load_state('networkidle')
        # shenqingrxm_list = []
        # 申请人列表
        shenqingrxm_list = ['武汉儿童医院']
        for shenqingrxm in shenqingrxm_list:
            for search_type in ['public','dljg']:
            # for search_type in ['sqr']:
            # search_type = 'public'
            # search_type = 'dljg'
            # search_type = 'sqr'
                print(f'开始抓取{shenqingrxm}_{search_type}')
                first_page_json = request_page_list(
                    context, 1, authorization, shenqingrxm, search_type)
                parse_page(first_page_json, shenqingrxm)
                print(f'{shenqingrxm}_第1页解析完成')
                max_page = first_page_json.get('data').get('pages')
                total = first_page_json.get('data').get('total')
                max_page = int(max_page)
                print(f'{shenqingrxm}_maxpage:{max_page}')
                print(f'{shenqingrxm}total:{total}')
                page.reload()
                if max_page > 1:
                    for page_num in range(2, max_page + 1):
                        page_json = request_page_list(
                            context, page_num, authorization, shenqingrxm, search_type)
                        parse_page(page_json, shenqingrxm)
                        print(f'{shenqingrxm}_第{page_num}页解析完成')
                        page.reload()
            # page.wait_for_load_state('networkidle')


if __name__ == '__main__':
    port_dict = {'XHHG': 'tmp_信合红谷接口查询',
                 'XH': 'tmp_协和端口查询', 'WB': "tmp_外部接口查询"}
    inst = 'XHHG'
    port = port_dict.get(inst)
    run_gonkai(inst, port)
    # run(inst)
    conn.close()
